package com.cennavi.minemap.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.cennavi.minemap.bean.ContactBean;
import com.cennavi.minemap.bean.MessageBean;
import com.cennavi.minemap.service.MailService;
import com.fasterxml.jackson.databind.ObjectMapper;

@RestController
@ComponentScan(basePackages = { "com.cennavi.minemap.service" })
@RequestMapping("/service/contact")
@PropertySource(value = { "classpath:application.properties" })
public class ContactUsController {
	@Autowired
	private MailService mailService;

	ObjectMapper objectMapper = new ObjectMapper();

	@Value("${spring.mail.username}")
	private String fromUser;

	@Value("${spring.mail.toUser}")
	private String toUser;

	@RequestMapping(value = "/send")
	public @ResponseBody MessageBean sendMail(@RequestBody String json) throws Exception {
		ContactBean userSourceBean = objectMapper.readValue(json, ContactBean.class);
		MessageBean messageBean = new MessageBean();
		String subject = "意见反馈：" + userSourceBean.getCompany() + "_" + userSourceBean.getUsername();
		String content = "公司名称：" + userSourceBean.getCompany() + "\r\n" + "所属行业：" + userSourceBean.getIndustry()
				+ "\r\n" + "所在城市：" + userSourceBean.getCity() + "\r\n" + "需求问题：" + userSourceBean.getQuestion() + "\r\n"
				+ "联系姓名：" + userSourceBean.getUsername() + "\r\n" + "联系电话：" + userSourceBean.getContact() + "\r\n"
				+ "联系邮箱：" + userSourceBean.getMail() + "\r\n";

		try {
			mailService.sendSimpleMail(fromUser, toUser.split(","), subject, content);
			messageBean.setErrcode(0);
		} catch (Exception e) {
			e.printStackTrace();
			messageBean.setErrcode(-1);
			messageBean.setErrmsg("send mail error");
		}
		return messageBean;
	}
}
